home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / DCLAP 6d / dclap6d / DClap / DTracker.cpp < prev    next >
Text File  |  1996-07-05  |  9KB  |  380 lines

  1. // DTracker.cpp
  2. // d.g.gilbert
  3.  
  4. // class DTracker  : public DCommand
  5. // mouse tracker, now used by DPanel and children
  6. // from MacApp TTracker
  7.  
  8. #include <ncbi.h>
  9. #include "DCommand.h"
  10. #include "DPanel.h"
  11. #include "DTracker.h"
  12.  
  13.  
  14.  
  15. static Nlm_PoinT gZeroPt = { 0, 0 };
  16.  
  17.  
  18. DTracker::DTracker()
  19. {
  20.     ITracker( 0, NULL, NULL, FALSE, FALSE, NULL);
  21. }  
  22.  
  23. DTracker::DTracker( long command, DTaskMaster* itsSource, char* itsTitle,
  24.                                Nlm_Boolean canUndo, Nlm_Boolean causesChange,
  25.                                DPanel* itsView)
  26. {
  27.     ITracker( command, itsSource, itsTitle, canUndo, causesChange, itsView);
  28. }
  29.  
  30.  
  31. void DTracker::ITracker( long command, DTaskMaster* itsSource, char* itsTitle,
  32.                                Nlm_Boolean canUndo, Nlm_Boolean causesChange,
  33.                                DPanel* itsView)
  34. {
  35.     fView= NULL;
  36.     fTrackNonMovement = FALSE;
  37.     fInitialPt = gZeroPt;
  38.     //fConstrainsMouse = FALSE;
  39.     //fScroller = NULL;
  40.     //fViewConstrain = TRUE;
  41.     //fDeskTopTrackingPort = NULL;
  42.     //fHysteresis = gZeroPt;
  43.  
  44.     this->ICommand( command, itsSource, itsTitle, canUndo, causesChange);
  45.     fView = itsView;
  46.     fCurrentTracker= this;
  47.  
  48.     Reset();
  49. }
  50.  
  51. void DTracker::SetTracker(DTracker* newTracker)
  52. {
  53.     fCurrentTracker= newTracker;
  54. }
  55.  
  56. void DTracker::Reset()
  57. {
  58.     fTrackPhase = trackBegin;
  59.     fAnchorPoint = gZeroPt;
  60.     fPreviousPoint = gZeroPt;
  61.     fNextPoint = gZeroPt;
  62.     fLastAnchorPoint = gZeroPt;
  63.     fLastPreviousPoint = gZeroPt;
  64.     fLastNextPoint = gZeroPt;
  65.     fMovedOnce = FALSE;    
  66. }
  67.  
  68. Nlm_Boolean DTracker::IsDoneTracking()
  69. {
  70.     if (fView) return fView->fMouseStillDown;
  71.     else return true;
  72. }  
  73.  
  74. #if 0
  75.     // this isn't any useful w/ Vibrant as long as it controls mouse tracking...
  76. void DTracker::Process() // override DTask::Process()
  77. {
  78.     DTracker* finalCommand = NULL;
  79.     DCommandHandler* handler = NULL;
  80.     Nlm_PoinT initpt= fInitialPt;
  81.     finalCommand = gApplication->TrackMouse(initpt, gStdHysteresis, this);
  82.         if (finalCommand) {
  83.         handler = finalCommand->fContext;
  84.         if (handler) handler->PerformCommand(finalCommand);
  85.         }
  86. }  
  87. #endif
  88.  
  89.  
  90.  
  91. // the DView/DPanel that owns this tracker does TrackMouse job (if aware)
  92.  
  93. DTracker* DTracker::TrackMouse(TrackPhase aTrackPhase,
  94.                                         Nlm_PoinT& anchorPoint, Nlm_PoinT& previousPoint,
  95.                                         Nlm_PoinT& nextPoint,    Nlm_Boolean mouseDidMove)
  96. {
  97.     fCurrentTracker= this;
  98.     if (fView)
  99.         fView->TrackMouse(aTrackPhase, anchorPoint, previousPoint, nextPoint, mouseDidMove);
  100.  
  101.     if (aTrackPhase == trackEnd) {
  102.         if (fView == NULL) return NULL;
  103.         else {    
  104.             Nlm_RecT viewr;
  105.             fView->ViewRect(viewr);
  106.             if (!Nlm_PtInRect(nextPoint, &viewr)) return NULL;
  107.             }
  108.         }
  109.     return fCurrentTracker;
  110. }
  111.  
  112.  
  113. // the DView/DPanel that owns this tracker does TrackFeedback job (if aware)
  114.  
  115. void DTracker::TrackFeedback(TrackPhase aTrackPhase,
  116.                                         const Nlm_PoinT& anchorPoint, const Nlm_PoinT& previousPoint,
  117.                                         const Nlm_PoinT& nextPoint, Nlm_Boolean mouseDidMove, Nlm_Boolean turnItOn)
  118. {
  119.     if (fView)
  120.         fView->TrackFeedback(aTrackPhase, anchorPoint, previousPoint, nextPoint, mouseDidMove, turnItOn);
  121. }  
  122.  
  123.  
  124.  
  125.  
  126. DTracker* DTracker::Start( Nlm_PoinT mouse)
  127. {
  128.     if (Nlm_dblClick) {
  129.         //?? return this; ??
  130.         }
  131.  
  132.     fTrackPhase = trackBegin;
  133.     fMovedOnce = FALSE;
  134.     fAnchorPoint = mouse;
  135.     fPreviousPoint = mouse;
  136.     fNextPoint = mouse;
  137.     if (fInitialPt.x == gZeroPt.x && fInitialPt.y == gZeroPt.y) fInitialPt= mouse; // dgg added
  138.     
  139.     this->BecomeTracker(NULL);
  140.     //this->ConstrainOnce(TRUE);
  141.     // in case Constrain changed the fNextPoint… guarantee that all 3 are the same on trackBegin
  142.     //fAnchorPoint = fNextPoint;
  143.     //fPreviousPoint = fNextPoint;
  144.  
  145.     DTracker* theTracker = this->TrackOnce(TRUE);
  146.     if (theTracker) {
  147.         // in case TrackMouse changed the fNextPoint… guarantee that all 3 are the same on trackBegin
  148.         theTracker->fAnchorPoint = theTracker->fNextPoint;
  149.         theTracker->fPreviousPoint = theTracker->fNextPoint;
  150.         theTracker->FeedbackOnce(TRUE, TRUE);
  151.         }
  152.  
  153.     return theTracker;
  154. }
  155.  
  156.  
  157. DTracker* DTracker::Continue( Nlm_PoinT mouse, Nlm_Boolean mouseMoved)
  158. {
  159.     fTrackPhase = trackContinue;                // supply trackphase to all interested parties
  160.     fNextPoint = mouse;
  161.  
  162.     if (!fMovedOnce) {
  163. #if 1
  164.         fMovedOnce= mouseMoved;
  165. #else
  166.         //this->ConstrainOnce(fPreviousPoint != fNextPoint);         
  167.         Nlm_PoinT amtMoved = fNextPoint - fAnchorPoint;
  168.         if ((abs((int)amtMoved.x) >= fHysteresis.x) || (abs((int)amtMoved.y) >= fHysteresis.y))
  169.             fMovedOnce = TRUE;
  170. #endif
  171.         }
  172.  
  173. #if FIX_LATER
  174.     Nlm_PoinT delta = gZeroPt;
  175.     if (fMovedOnce || fTrackNonMovement) {
  176.         if (fScroller && fView)     {
  177.             // convert fNextPoint to scroller coordinates
  178.             Nlm_PoinT mouseInScroller = fNextPoint;
  179.             fView->LocalToWindow(mouseInScroller);
  180.             fScroller->WindowToLocal(mouseInScroller);
  181.  
  182.             // AutoScroll if the CPoint is outside of the scroller's extent
  183.             Nlm_PoinT autoScrollLimit;
  184.             fScroller->GetExtent(autoScrollLimit);
  185.             if (!autoScrollLimit.Contains(mouseInScroller)) {
  186.                 fScroller->AutoScroll(mouseInScroller, delta);// Get the amount to autoscroll
  187.                 fNextPoint += delta;
  188.                 }
  189.             }
  190.         //this->ConstrainOnce(fPreviousPoint != fNextPoint);
  191.         }
  192. #endif
  193.  
  194.     Boolean willScroll = false; //delta != gZeroPt;
  195.     Boolean didMove = (fPreviousPoint.x != fNextPoint.x || fPreviousPoint.y != fNextPoint.y);
  196.  
  197.     this->FeedbackOnce( didMove || willScroll, FALSE);
  198.  
  199. #if FIX_LATER
  200.     if (willScroll) {
  201.         this->AutoScroll(delta);            // OK, now actually do the scrolling 
  202.         if (fView) fView->Update();        // Keep synchronized. ScrollDraw only
  203.         //this->DoFocus();     
  204.         }
  205. #endif
  206.  
  207.     DTracker* theTracker = this->TrackOnce(didMove);
  208.     if (theTracker) {
  209.         theTracker->FeedbackOnce( didMove || willScroll, TRUE);
  210.         theTracker->fPreviousPoint = theTracker->fNextPoint;
  211.         }
  212.  
  213.     return theTracker;
  214. }  
  215.  
  216.  
  217.  
  218. DTracker* DTracker::Finish( Nlm_PoinT mouse)
  219. {
  220.     fTrackPhase = trackEnd;                     
  221.     fNextPoint = mouse;
  222.  
  223.     this->FeedbackOnce(TRUE, FALSE);
  224.     DTracker* theTracker = this->TrackOnce(TRUE);
  225.     //if (theTracker) theTracker->CleanUpFocus();
  226.     return theTracker;
  227. }  
  228.  
  229.  
  230.  
  231.  
  232.  
  233. DTracker* DTracker::TrackOnce( Nlm_Boolean didMouseMove)
  234. {
  235.     DView* oldView = fView;
  236.     Nlm_PoinT anchorPoint = fAnchorPoint;
  237.     Nlm_PoinT previousPoint = fPreviousPoint;
  238.     Nlm_PoinT nextPoint = fNextPoint;
  239.  
  240.     DTracker * newTracker = 
  241.         this->TrackMouse(fTrackPhase, anchorPoint, previousPoint, nextPoint, didMouseMove);
  242.     
  243.     fAnchorPoint = anchorPoint;
  244.     fPreviousPoint = previousPoint;
  245.     fNextPoint = nextPoint;
  246.     
  247.     if (newTracker == this) {
  248.         if (this->fView != oldView) this->BecomeTracker(this);
  249.         }
  250.     else {
  251.         if (newTracker) newTracker->BecomeTracker(this);
  252.         //else this->CleanUpFocus();
  253.         this->suicide(); //Free();
  254.     }
  255.  
  256.     return newTracker;
  257.  
  258.  
  259. void DTracker::BecomeTracker(DTracker* oldTracker)
  260. {
  261.     if (oldTracker) {
  262.         //fDeskTopTrackingPort = oldTracker->fDeskTopTrackingPort;
  263.         //fHysteresis = oldTracker->fHysteresis;
  264.         fTrackPhase = oldTracker->fTrackPhase;
  265.         fAnchorPoint = oldTracker->fAnchorPoint;
  266.         fPreviousPoint = oldTracker->fPreviousPoint;
  267.         fNextPoint = oldTracker->fNextPoint;
  268.         fLastAnchorPoint = oldTracker->fLastAnchorPoint;
  269.         fLastPreviousPoint = oldTracker->fLastPreviousPoint;
  270.         fLastNextPoint = oldTracker->fLastNextPoint;
  271.         }
  272.  
  273.     //this->DoFocus();
  274. }  
  275.  
  276. void DTracker::FeedbackOnce( Boolean mouseDidMove, Boolean turnItOn)
  277. {
  278.     Nlm_PoinT ancr, prev, next;
  279.     //Nlm_InvertMode();
  280.     
  281.     if (turnItOn) {
  282.         ancr= fAnchorPoint;
  283.         prev= fPreviousPoint;
  284.         next= fNextPoint;
  285.         this->TrackFeedback( fTrackPhase, ancr, prev, next, mouseDidMove, turnItOn);
  286.         // save these to turn it off with
  287.         if (mouseDidMove) {
  288.             fLastAnchorPoint = fAnchorPoint;
  289.             fLastPreviousPoint = fPreviousPoint;
  290.             fLastNextPoint = fNextPoint;
  291.             }
  292.         }
  293.     else {
  294.         ancr= fLastAnchorPoint;
  295.         prev= fLastPreviousPoint;
  296.         next= fLastNextPoint;
  297.         this->TrackFeedback( fTrackPhase, ancr, prev, next, mouseDidMove, turnItOn);
  298.         }
  299. }  
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306. #if 0
  307.  
  308. // usage: ...
  309.  
  310.  
  311. somewhere()
  312. {
  313.     // macapp process:
  314.     //finalCommand = gApplication->TrackMouse(fInitialPt.Copy(), gStdHysteresis, this);
  315.     // get handler of finalcommand & call its processor
  316.     
  317.     //vibrant process:
  318.     aDPanel->SetTracker( new DTracker(blah));
  319.     // handle mouse...
  320.     
  321.     // do this in DPanel::Release() call...
  322.     if (aDPanel->IsDoneTracking()) {
  323.         finalCommand= aDPanel->fTracker; 
  324.         if (finalCommand) finalCommand->Process();
  325.         }
  326.     
  327. }
  328.  
  329. DTracker* DApplication::TrackMouse( Nlm_PoinT theMouse, DTracker* theCommand)
  330. {
  331.     DTracker* currentTracker = NULL;     
  332.  
  333.     currentTracker = theCommand->HandleTrackBegin( theMouse); // == Click
  334.  
  335.     while (currentTracker && !currentTracker->IsDoneTracking())
  336.         currentTracker = currentTracker->HandleTrackContinue(); // == Drag & Hold
  337.  
  338.     if (currentTracker)
  339.         currentTracker = currentTracker->HandleTrackEnd();  // == Release
  340.  
  341.     return currentTracker;
  342. }  
  343.  
  344.  
  345. void DPanel::SetTracker( DTracker* mouseTracker)
  346. {
  347.     fTracker= mouseTracker;
  348. }
  349.  
  350. void DPanel::Click(Nlm_PoinT mouse)
  351. {
  352.     // this is always called before Drag/Hold/Release
  353.     // Release is only called if Drag/Hold are called (only if mouse is down for a while)
  354.     fMouseStillDown= false;
  355.     if (fTracker) fTracker= fTracker->Start( mouse);
  356. }
  357.  
  358. void DPanel::Drag(Nlm_PoinT mouse)
  359. {
  360.     fMouseStillDown= true;
  361.     if (fTracker) fTracker= fTracker->Continue( mouse, true);
  362. }
  363.  
  364. void DPanel::Hold(Nlm_PoinT mouse)
  365. {
  366.     fMouseStillDown= true;
  367.     if (fTracker) fTracker= fTracker->Continue( mouse, false);
  368. }
  369.  
  370. void DPanel::Release(Nlm_PoinT mouse)
  371. {
  372.     fMouseStillDown= false;
  373.     if (fTracker) fTracker= fTracker->Finish( mouse);
  374. }
  375.  
  376. #endif
  377.  
  378.  
  379.